package com.admin.web.controller;

import com.admin.web.model.AjaxJson;
import com.admin.web.uresponse.OBGUserResponse;
import com.bayss.core.constant.ResultMsg;
import com.bayss.core.entity.coupon.Coupon;
import com.bayss.core.service.coupon.CouponService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 券管理
 */
@RestController
public class CouponController extends ApiBaseController {
    @Autowired
    private CouponService couponService;

    /**
     * 优惠券删除
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/coupon/delete.json")
    public AjaxJson deleteById(Integer id) {
        AjaxJson ajaxJson = new AjaxJson();
        try {
            couponService.deleteById(id);
            ajaxJson.setStatus(200).setMsg("提交成功");
        } catch (Exception e) {
            e.printStackTrace();
            ajaxJson.setStatus(250).setMsg("提交失败");
        }
        return ajaxJson;
    }

    /**
     * 优惠券新增
     *
     * @param coupon
     * @return
     */
    @PostMapping(value = "/coupon/add.json")
    public ResultMsg add(OBGUserResponse obgUserResponse,@RequestBody Coupon coupon) {
        coupon.setOperatorId(obgUserResponse.getUid());
        coupon.setCreator(obgUserResponse.getObgUserInfo().getNickname());
        return couponService.addCoupon(coupon);
    }

    /**
     * 优惠券更新
     *
     * @param coupon
     * @return
     */
    @PostMapping(value = "/coupon/update.json")
    public ResultMsg update(OBGUserResponse obgUserResponse,@RequestBody Coupon coupon) {
        coupon.setOperatorId(obgUserResponse.getUid());
        coupon.setOperatorName(obgUserResponse.getObgUserInfo().getNickname());
        couponService.updateCoupon(coupon);
        return ResultMsg.success();
    }

    /**
     * 优惠券分页查询
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping(value = "/coupon/getByPage.json")
    public ResultMsg getByPage(Integer page, Integer pageSize) {
        try {
            PageInfo<Coupon> pageInfo = couponService.getByPage(page, pageSize);
            return ResultMsg.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.fail(-1, "查询失败");
        }
    }

    /**
     * 优惠券查询
     *
     * @param discountType 1,"满减"2,"打折"
     */
    @GetMapping(value = "/coupon/list.json")
    public ResultMsg list(Integer discountType) {
        try {
            List<Coupon> list = couponService.list(discountType);
            return ResultMsg.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.fail(-1, "查询失败");
        }
    }


    /**
     * 优惠券发放
     */
    @PostMapping(value = "/coupon/addCouponUser.json")
    public ResultMsg addCouponUser(String couponIds, String userIds) {
        try {
            couponService.addCouponUser(couponIds, userIds);
            return ResultMsg.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.fail(-1, "操作失败");
        }
    }

    /**
     * 优惠券查询
     */
    @GetMapping(value = "/coupon/getCouponUser.json")
    public ResultMsg getCouponUser(Integer uid) {
        try {
            return ResultMsg.success(couponService.findUserCoupon(uid));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.fail(-1, "操作失败");
        }
    }
}
